package cn.com.taiji.pos.controller;

import cn.com.taiji.pos.common.util.MD5;
import cn.com.taiji.pos.entity.CardAccount;
import cn.com.taiji.pos.entity.CardCustomer;
import cn.com.taiji.pos.entity.CardInfo;
import cn.com.taiji.pos.service.ICardAccountService;
import cn.com.taiji.pos.service.ICardCustomerService;
import cn.com.taiji.pos.service.ICardInfoService;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.dreamyoung.mprelation.AutoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/Customer")
public class CustomerController {

    @Autowired
    private ICardCustomerService cardCustomerService;
    @Autowired
    private ICardInfoService cardInfoService;
    @Autowired
    private ICardAccountService cardAccountService;
    @Autowired
    private AutoMapper autoMapper;

    /**
     * 账户查询
     *
     * @param request
     * @param response
     */
    @RequestMapping(value = "/searchCustomerAccount", method = RequestMethod.POST)
    public String searchCustomer(Model model, HttpServletRequest request, HttpServletResponse response) {
        model.addAttribute("result", "fail");

        String cardCode = request.getParameter("cardCode");//卡号
        String customerNo = request.getParameter("customerNo");//顾客编号
        String customerName = request.getParameter("customerName");//顾客姓名
        if (cardCode.contains("&")) {
            //虚拟二维码支付
            String[] split = cardCode.split("&");
            if (split.length != 4) {
                model.addAttribute("message", "二维码无效");
                return JSONObject.toJSONString(model, SerializerFeature.IgnoreNonFieldGetter);
            } else if (LocalDateTime.now().isAfter(LocalDateTime.parse(split[1], DateTimeFormatter.ofPattern("yyMMddHHmmss")).plusMinutes(2))) {
                model.addAttribute("message", "二维码已过期");
                return JSONObject.toJSONString(model, SerializerFeature.IgnoreNonFieldGetter);
            } else if (split[0].equals(MD5.MD5Encode(split[3] + split[2] + split[1]).toUpperCase())) {
                //验证通过
                customerNo = split[3];
                cardCode = "";
            } else {
                model.addAttribute("message", "二维码无效");
                return JSONObject.toJSONString(model, SerializerFeature.IgnoreNonFieldGetter);
            }
        }
        if (StringUtils.hasText(cardCode)) {
            //根据卡片编号直接查询
            CardInfo cardInfo = cardInfoService.findByCardCode(cardCode);
            if (cardInfo == null) {
                model.addAttribute("message", "该卡片未关联用户");
                return JSONObject.toJSONString(model);
            }
            model.addAttribute("cardAccountList", autoMapper.mapperEntityList(cardAccountService.findByCustomerId(cardInfo.getCustomerId())));
        } else if (StringUtils.hasText(customerNo) || StringUtils.hasText(customerName)) {
            //根据姓名,顾客编号查询
            QueryWrapper<CardCustomer> cardCustomerWrapper = new QueryWrapper<>();
            cardCustomerWrapper.eq("STATUS", 0);
            if (StringUtils.hasText(customerNo)) {
                cardCustomerWrapper.eq("CUSTOMER_NO", customerNo);
            }
            if (StringUtils.hasText(customerName)) {
                cardCustomerWrapper.like("NAME", customerName);
            }
            cardCustomerWrapper.orderByDesc("MOBILE");
            List<CardCustomer> cardCustomerList = cardCustomerService.list(cardCustomerWrapper);
            if (cardCustomerList == null || cardCustomerList.size() == 0) {
                model.addAttribute("message", "未查询到相关用户");
                return JSONObject.toJSONString(model);
            }
            ArrayList<CardAccount> cardAccountList = new ArrayList<>();
            for (CardCustomer cardCustomer : cardCustomerList) {
                cardAccountList.addAll(autoMapper.mapperEntityList(cardAccountService.findByCustomerId(cardCustomer.getId())));
            }
            model.addAttribute("cardAccountList", cardAccountList);
        } else {
            //未填写查询条件
            model.addAttribute("message", "请检查查询条件");
            return JSONObject.toJSONString(model);
        }
        model.addAttribute("result", "success");
        model.addAttribute("message", "成功");
        SimplePropertyPreFilter filter = new SimplePropertyPreFilter("MOBILE", "id", "message", "result", "cardAccountList", "cardInfo", "cardNo", "customerNo", "amount", "name", "customer");
        return JSONObject.toJSONString(model, filter, SerializerFeature.DisableCircularReferenceDetect);
    }


}
